package com.avic.rong.adapter.balance;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.bean.mine.BzMemberCardBean;
import com.avic.rong.bean.mine.MembershipCardBalanceBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RecvBzMemberBalanceAdapter extends RecyclerView.Adapter {


    private ArrayList<BzMemberCardBean> mList;
    private Context mContext;

    public RecvBzMemberBalanceAdapter(ArrayList<BzMemberCardBean> mList, Context mContext) {
        this.mList = mList;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_capital, viewGroup, false);
        return new MemberBalanceHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        BzMemberCardBean cardBalanceBean = mList.get(i);
        MemberBalanceHolder memberBalanceHolder = (MemberBalanceHolder) viewHolder;
        memberBalanceHolder.tv_desc.setText(cardBalanceBean.getPayDesc());

        String s = String.valueOf(cardBalanceBean.getPayTime());
        StringBuffer stringBuffer = new StringBuffer();
        StringBuffer append = stringBuffer.append(s).append("000");
        Long aLong = Long.valueOf(append.toString());

        memberBalanceHolder.tv_date.setText(longToDate(aLong));

        if (cardBalanceBean.getSign() == 0){
            memberBalanceHolder.tv_price.setText("-￥"+cardBalanceBean.getPayPoints());
        }else if(cardBalanceBean.getPayPoints() == 1){
            memberBalanceHolder.tv_price.setTextColor(mContext.getResources().getColor(R.color.red_dot_color));
            memberBalanceHolder.tv_price.setText("+￥"+cardBalanceBean.getPayPoints());
        }

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MemberBalanceHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_desc)
        TextView tv_desc;
        @BindView(R.id.tv_date)
        TextView tv_date;
        @BindView(R.id.tv_price)
        TextView tv_price;
        public MemberBalanceHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
    public static String longToDate(long lo){
        Date date = new Date(lo);
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sd.format(date);
    }
}
